﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using BusinessLayer.Tables;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
/// <summary>
/// Summary description for InterPos
/// </summary>
public class InterPos : IPos
{
    public InterPos()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region Fields
    Db_Stok stok;
    PromoDetails details;
    PromoHeader header;
    Db_Reyon reyon;
    Db_Musteri Customer;
    Db_Barkot barkod_;
    Db_Poslar pos;
    DataTable dtHeader;
    DataTable dtHeader2;
    DataTable dtDetails;
    DataTable dtDetails2;
    DataTable dtStok;
    DataTable dtReyon;
    DataTable dtCustomer;
    DataTable dt;
    DataTable dtDepoPos;
    DataSet dsDepoPos;
    #endregion

    #region IPos Members

    public byte[] PromosyonDosyasi()
    {
        header = new PromoHeader();
        details = new PromoDetails();
        dtHeader = header.GetAllRecord_Datatable2("SELECT proh_kod, proh_adi, proh_cinsi, proh_tip, proh_alisverislimit, proh_indirimorani, proh_indirimtutari, proh_odemetipi, proh_urunlimit, proh_promoadet, proh_promofiyat, proh_promourun,proh_mus_ind_dahil, proh_baslangic_tarihi, proh_bitis_tarihi FROM PROMOHEADER");
        List<string> sbYaz = new List<string>();
        if (dtHeader.Rows.Count > 0)
        {
            for (int headerTableForMain = 0; headerTableForMain < dtHeader.Rows.Count; headerTableForMain++)
            {
                string AlisVerisLimit = string.Empty;
                string IndirimOrani = string.Empty;
                string IndirimTutari = string.Empty;
                string OdemeTipi = string.Empty;
                string MH = string.Empty;
                string Adet = string.Empty;
                string Plnu = string.Empty;
                string VPlnu = string.Empty;
                string UrunLimit = string.Empty;
                string UrunFiyat = string.Empty;
                string DonenKdv = string.Empty;
                decimal Format = decimal.Zero;
                string MustIndOran = string.Empty;
                string ReyonNo = string.Empty;
                switch (dtHeader.Rows[headerTableForMain][2].ToString())
                {
                    case "0":
                        if (dtHeader.Rows[headerTableForMain]["proh_alisverislimit"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_alisverislimit"].ToString());
                            AlisVerisLimit = Format.ToString("0000000.00");
                            AlisVerisLimit = AlisVerisLimit.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString());
                            IndirimOrani = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_indirimtutari"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_indirimtutari"].ToString());
                            IndirimTutari = Format.ToString("0000000.00");
                            IndirimTutari = IndirimTutari.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_odemetipi"].ToString() == "0")
                            OdemeTipi = "N";
                        else
                            OdemeTipi = "H";
                        if (dtHeader.Rows[headerTableForMain]["proh_tip"].ToString() == "1")
                        {
                            MH = "M";
                            dtDetails = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM  PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 0)");
                            if (dtDetails.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails.Rows.Count; i++)
                                {
                                    if (dtDetails.Rows[i][0].ToString() != "")
                                        sbYaz.Add("P1" + "," + MH + "," + AlisVerisLimit + "," + IndirimOrani + "," + IndirimTutari + "," + dtDetails.Rows[i][0].ToString() + "," + OdemeTipi);
                                }
                            }
                        }
                        else
                        {
                            MH = "H";
                            sbYaz.Add("P1" + "," + MH + "," + AlisVerisLimit + "," + IndirimOrani + "," + IndirimTutari + "," + "," + OdemeTipi);
                        }
                        break;
                    case "1":
                        if (dtHeader.Rows[headerTableForMain]["proh_promoadet"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_promoadet"].ToString());
                            Adet = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString());
                            IndirimOrani = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_indirimtutari"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_indirimtutari"].ToString());
                            IndirimTutari = Format.ToString("000000.00");
                            IndirimTutari = IndirimTutari.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_urunlimit"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_urunlimit"].ToString());
                            UrunLimit = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_tip"].ToString() == "1")
                        {
                            MH = "M";
                            dtDetails = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM  PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 0)");
                            if (dtDetails.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails.Rows.Count; i++)
                                {
                                    if (dtDetails.Rows[i][0].ToString() != "")
                                    {
                                        dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 1)");
                                        if (dtDetails2.Rows.Count > 0)
                                        {
                                            for (int detailsFor3 = 0; detailsFor3 < dtDetails2.Rows.Count; detailsFor3++)
                                            {
                                                Plnu = StokRecid(dtDetails2.Rows[detailsFor3][0].ToString());
                                                sbYaz.Add("P2" + "," + MH + "," + Adet + "," + Plnu + "," + IndirimOrani + "," + IndirimTutari + "," + UrunLimit + "," + dtDetails.Rows[i][0].ToString());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            MH = "H";
                            dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 1)");
                            if (dtDetails2.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails2.Rows.Count; i++)
                                    Plnu = StokRecid(dtDetails2.Rows[i][0].ToString());
                                sbYaz.Add("P2" + "," + MH + "," + Adet + "," + Plnu + "," + IndirimOrani + "," + IndirimTutari + "," + UrunLimit);
                            }
                        }
                        break;
                    case "2":
                        if (dtHeader.Rows[headerTableForMain]["proh_urunlimit"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_urunlimit"].ToString());
                            UrunLimit = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_promoadet"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_promoadet"].ToString());
                            Adet = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_promofiyat"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_promofiyat"].ToString());
                            UrunFiyat = Format.ToString("0000000.00");
                            UrunFiyat = UrunFiyat.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_tip"].ToString() == "1")
                        {
                            MH = "M";
                            dtHeader2 = header.GetAllRecord_Datatable2("SELECT proh_promourun FROM PROMOHEADER WHERE (proh_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "')");
                            if (dtHeader2.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtHeader2.Rows.Count; i++)
                                    VPlnu = VPlnuRecid(dtHeader2.Rows[i][0].ToString());
                            }
                            dtDetails = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM  PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 0)");
                            if (dtDetails.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails.Rows.Count; i++)
                                {
                                    dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM PROMODETAILS WHERE (prod_kod = N'" + dtDetails.Rows[i]["proh_kod"].ToString() + "') AND (prod_tip = 1)");
                                    if (dtDetails2.Rows.Count > 0)
                                    {
                                        for (int i2 = 0; i2 < dtDetails2.Rows.Count; i2++)
                                            Plnu = StokRecid(dtDetails2.Rows[i2][0].ToString());
                                    }
                                    if (dtDetails.Rows[i][0].ToString() != "")
                                        sbYaz.Add("P3" + "," + Plnu + "," + UrunLimit + "," + VPlnu + "," + Adet + "," + UrunFiyat + "," + dtDetails.Rows[i][0].ToString());
                                }
                            }
                        }
                        else
                        {
                            MH = "H";
                            dtHeader2 = header.GetAllRecord_Datatable2("SELECT proh_promourun FROM PROMOHEADER WHERE (proh_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "')");
                            if (dtHeader2.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtHeader2.Rows.Count; i++)
                                    VPlnu = StokRecid(dtHeader2.Rows[i][0].ToString());
                            }
                            dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 1)");
                            if (dtDetails2.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails2.Rows.Count; i++)
                                    Plnu = StokRecid(dtDetails2.Rows[i][0].ToString());
                                sbYaz.Add("P3" + "," + Plnu + "," + UrunLimit + "," + VPlnu + "," + Adet + "," + UrunFiyat);
                            }
                        }
                        break;
                    case "3":
                        if (dtHeader.Rows[headerTableForMain]["proh_alisverislimit"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_alisverislimit"].ToString());
                            AlisVerisLimit = Format.ToString("0000000.00");
                            AlisVerisLimit = AlisVerisLimit.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_promoadet"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_promoadet"].ToString());
                            Adet = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        dtHeader2 = header.GetAllRecord_Datatable2("SELECT proh_promourun FROM PROMOHEADER WHERE (proh_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "')");
                        if (dtHeader2.Rows.Count > 0)
                        {
                            for (int i = 0; i < dtHeader2.Rows.Count; i++)
                                VPlnu = StokRecid(dtHeader2.Rows[i][0].ToString());
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_promofiyat"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_promofiyat"].ToString());
                            UrunFiyat = Format.ToString("0000000.00");
                            UrunFiyat = UrunFiyat.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_tip"].ToString() == "1")
                        {
                            MH = "M";
                            dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM  PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 0)");
                            if (dtDetails2.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails2.Rows.Count; i++)
                                {
                                    if (dtDetails2.Rows[i][0].ToString() != "")
                                        sbYaz.Add("P4" + "," + MH + "," + AlisVerisLimit + "," + Adet + "," + Plnu + "," + UrunFiyat + "," + dtDetails2.Rows[i][0].ToString());
                                }
                            }
                        }
                        else
                        {
                            MH = "H";
                            sbYaz.Add("P4" + "," + MH + "," + AlisVerisLimit + "," + Adet + "," + Plnu + "," + UrunFiyat);
                        }
                        break;
                    case "4":
                        if (dtHeader.Rows[headerTableForMain]["proh_alisverislimit"].ToString() != "")
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_alisverislimit"].ToString());
                            AlisVerisLimit = Format.ToString("0000000.00");
                            AlisVerisLimit = AlisVerisLimit.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_odemetipi"].ToString() == "0")
                            DonenKdv = Kdv("0", dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString());
                        else
                            DonenKdv = Kdv("1", dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString());
                        if (dtHeader.Rows[headerTableForMain]["proh_tip"].ToString() == "1")
                        {
                            MH = "M";
                            dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM  PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 0)");
                            if (dtDetails2.Rows.Count > 0)
                            {
                                for (int i = 0; i < dtDetails2.Rows.Count; i++)
                                {
                                    if (dtDetails2.Rows[i][0].ToString() != "")
                                        sbYaz.Add("P5" + "," + MH + "," + AlisVerisLimit + "," + DonenKdv + "," + dtDetails2.Rows[i][0].ToString());
                                }
                            }
                        }
                        else
                        {
                            MH = "H";
                            sbYaz.Add("P5" + "," + MH + "," + AlisVerisLimit + "," + DonenKdv);
                        }
                        break;
                    case "5":
                        if (dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString() == "0")
                            IndirimOrani = "";
                        else
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_indirimorani"].ToString());
                            IndirimOrani = Format.ToString("00");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_indirimtutari"].ToString() == "0")
                            IndirimTutari = "----------";
                        else
                        {
                            Format = decimal.Parse(dtHeader.Rows[headerTableForMain]["proh_indirimtutari"].ToString());
                            IndirimTutari = Format.ToString("0000000.00");
                            IndirimTutari = IndirimTutari.Replace(",", ".");
                            Format = decimal.Zero;
                        }
                        if (dtHeader.Rows[headerTableForMain]["proh_mus_ind_dahil"].ToString() == "True")
                            MustIndOran = "E";
                        else
                            MustIndOran = "H";
                        dtDetails = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM PROMODETAILS WHERE (prod_tip = 2) AND (prod_kod = N'" + dtHeader.Rows[headerTableForMain][0].ToString() + "')");
                        if (dtDetails.Rows.Count > 0)
                        {
                            for (int i1 = 0; i1 < dtDetails.Rows.Count; i1++)
                            {
                                ReyonNo = ReyonNoDondur(dtDetails.Rows[i1][0].ToString());
                                if (ReyonNo != "")
                                {
                                    if (dtHeader.Rows[headerTableForMain]["proh_tip"].ToString() == "1")
                                    {
                                        MH = "M";
                                        dtDetails2 = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM  PROMODETAILS WHERE (prod_kod = N'" + dtHeader.Rows[headerTableForMain]["proh_kod"].ToString() + "') AND (prod_tip = 0)");
                                        if (dtDetails2.Rows.Count > 0)
                                        {
                                            for (int i = 0; i < dtDetails2.Rows.Count; i++)
                                            {
                                                if (dtDetails2.Rows[i][0].ToString() != "")
                                                {
                                                    if (IndirimOrani != "")
                                                        sbYaz.Add("P6" + "," + MH + "," + ReyonNo + "," + IndirimOrani + "," + IndirimTutari + "," + MustIndOran + "," + dtDetails2.Rows[i][0].ToString());
                                                    else
                                                        sbYaz.Add("P6" + "," + MH + "," + ReyonNo + "," + IndirimTutari + "," + MustIndOran + "," + dtDetails2.Rows[i][0].ToString());
                                                }
                                            }

                                        }
                                    }

                                    else
                                    {
                                        MH = "H";
                                        if (IndirimOrani != "")
                                            sbYaz.Add("P6" + "," + MH + "," + ReyonNo + "," + IndirimOrani + "," + IndirimTutari + "," + MustIndOran);
                                        else
                                            sbYaz.Add("P6" + "," + MH + "," + ReyonNo + "," + IndirimTutari + "," + MustIndOran);
                                    }
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < sbYaz.Count; i++)
            sb.Append(sbYaz[i] + Environment.NewLine);
        return GZipCompress(sb.ToString());

    }

    public byte[] GZipCompress(string sb)
    {
        byte[] test = System.Text.Encoding.ASCII.GetBytes(sb);

        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                gzip.Write(test, 0, test.Length);
            }
            byte[] compressedBuffer = ms.ToArray();
            return compressedBuffer;
        }
    }

    public string StokRecid(string kod)
    {
        stok = new Db_Stok();
        string donen = "";
        decimal format = 0;
        dtStok = stok.GetAllRecord_Datatable2("SELECT stok_id FROM TBL_0_STOK WHERE (stok_adi = N'" + kod + "')");
        if (dtStok.Rows.Count > 0)
        {
            for (int i = 0; i < dtStok.Rows.Count; i++)
                donen = dtStok.Rows[i][0].ToString();
            if (donen != "")
                format = decimal.Parse(donen);
            donen = format.ToString("000000");
        }
        return donen;
    }

    public string StokRecidAndReyonid(string kod)
    {
        stok = new Db_Stok();
        string stokid = "";
        string reyonid = "";
        decimal format = 0;
        int fark = 0;
        string bosluk = string.Empty;
        StringBuilder sb = new StringBuilder();
        dtStok = stok.GetAllRecord_Datatable2("SELECT TBL_0_STOK.stok_id, TBL_0_REYON.ryn_reyon_kod FROM TBL_0_REYON INNER JOIN TBL_0_STOK ON TBL_0_REYON.ryn_rec_id = TBL_0_STOK.stok_reyon_id WHERE (TBL_0_REYON.ryn_reyon_ad = N'" + kod + "')");
        if (dtStok.Rows.Count > 0)
        {
            for (int i = 0; i < dtStok.Rows.Count; i++)
            {
                stokid = dtStok.Rows[i][0].ToString();
                reyonid = dtStok.Rows[i][1].ToString();
            }
        }
        if (reyonid.Length < 4)
        {
            fark = 4 - reyonid.Length;
            for (int i = 0; i < fark; i++)
                bosluk += " ";
            reyonid = reyonid + bosluk;
        }
        if (stokid != "")
        {
            format = decimal.Parse(stokid);
            stokid = format.ToString("000000");
            sb.Append(stokid + "," + reyonid);
        }
        return sb.ToString();
    }

    public string VPlnuRecid(string kod)
    {
        stok = new Db_Stok();
        string donen = string.Empty;
        decimal fark = 0;

        dtStok = stok.GetAllRecord_Datatable2("SELECT stok_id FROM TBL_0_STOK WHERE (stok_kodu = N'" + kod + "')");
        if (dtStok.Rows.Count > 0)
        {
            for (int i = 0; i < dtStok.Rows.Count; i++)
                donen = dtStok.Rows[i][0].ToString();
        }
        if (donen != "")
        {
            fark = decimal.Parse(donen);
            donen = fark.ToString("00000");
        }
        return donen;
    }

    public string Kdv(string kod, string indirimorani)
    {
        string sonuc = "";
        if (kod == "0" && indirimorani != "")
            sonuc = "N/" + indirimorani + "," + "K/1";
        else if (kod == "1" && indirimorani != "")
            sonuc = "N/1" + "," + "K/" + indirimorani;
        else
            sonuc = "N/1,K/1";
        return sonuc;
    }

    public string ReyonNoDondur(string kod)
    {
        reyon = new Db_Reyon();
        stok = new Db_Stok();
        string sonuc = string.Empty;
        string bosluk = string.Empty;
        int fark = 0;
        dtReyon = stok.GetAllRecord_Datatable2("SELECT ryn_reyon_kod FROM TBL_0_REYON WHERE (ryn_reyon_ad = N'" + kod + "')");
        if (dtReyon.Rows.Count > 0)
        {
            for (int i = 0; i < dtReyon.Rows.Count; i++)
                sonuc = dtReyon.Rows[i][0].ToString();
        }
        if (sonuc.Length < 4)
        {
            fark = 4 - sonuc.Length;
            for (int i = 0; i < fark; i++)
                bosluk += " ";
            sonuc = sonuc + bosluk;
            bosluk = "";
            fark = 0;
        }
        return sonuc;
    }

    public byte[] CreateReyonDAT()
    {
        details = new PromoDetails();
        string UrunPlno = string.Empty;
        StringBuilder sb = new StringBuilder();
        dtDetails = details.GetAllRecord_Datatable2("SELECT prod_promokod FROM PROMODETAILS WHERE (prod_tip = 2)");
        if (dtDetails.Rows.Count > 0)
        {
            for (int i = 0; i < dtDetails.Rows.Count; i++)
                sb.Append(StokRecidAndReyonid(dtDetails.Rows[i]["prod_promokod"].ToString()) + "\r\n");
        }
        return GZipCompress(sb.ToString());
    }
    #endregion

    #region Hareket File
    public void HareketFileSave(DataSet ds)
    {
        if (ds.Tables[0].Rows.Count > 0)
        {

        }
    }

    #endregion

    #region Inter Pos Cari
    public DataTable CariCek()
    {
        Customer = new Db_Musteri();
        dtCustomer = Customer.GetAllRecord_Datatable2("SELECT rec_id, musteri_kod,musteri_adi,(is_mahalle + ' Mah.' + is_cadde + ' Cd.' + is_sokak + ' Sk. No:' + cast(is_no as varchar) + is_il + ' / ' + is_ilce) as adres, vergi_dairesi , vergi_numarasi,'0' as Disc FROM TBL_0_MUSTERI ORDER BY musteri_kod ");
        return dtCustomer;
    }

    public byte[] InterPOS_Cari_Exp(DataTable cariler)
    {
        StringBuilder sbCariFile = new StringBuilder();
        StringBuilder sbCariNoFile = new StringBuilder();
        StringBuilder sbCariKodFile = new StringBuilder();
        StringBuilder AllStrings = new StringBuilder();
        try
        {
            DataSet ds = new DataSet();
            string CariId = "";
            string CariKod = "";
            string CariUnvan = "";
            string CariSatir = "";
            string Adres = "";
            string VDaire = "";
            string Vno = "";
            string Discount = "";
            string Toplam_Satir = "";
            int sira = 1;
            DataSet Sorted_dataset = new DataSet();
            Sorted_dataset.Tables.Add("CARI");
            Sorted_dataset.Tables["CARI"].Columns.Add("ID", typeof(string));
            Sorted_dataset.Tables["CARI"].Columns.Add("SIRANO", typeof(string));
            for (int i = 0; i < cariler.Rows.Count; i++)
            {
                CariId = Strings.Format(Convert.ToInt32(cariler.Rows[i][0]), "000000");
                CariKod = Strings.Mid(cariler.Rows[i][1].ToString(), 1, 20);
                if (Strings.Len(CariKod) < 20)
                    CariKod = CariKod + Strings.Space(20 - Strings.Len(CariKod));

                CariUnvan = cariler.Rows[i][2].ToString();

                if (string.IsNullOrEmpty(cariler.Rows[i][3].ToString()) == false)
                    Adres = cariler.Rows[i][3].ToString();
                else
                    Adres = "";

                CariSatir = CariUnvan + " " + Adres;

                if (Strings.Len(CariSatir) < 80)
                    CariSatir = CariSatir + Strings.Space(80 - Strings.Len(CariSatir));
                else
                    CariSatir = Strings.Mid(CariSatir, 1, 80);

                VDaire = Strings.Mid(cariler.Rows[i][4].ToString(), 1, 15);
                if (Strings.Len(VDaire) < 15)
                    VDaire = VDaire + Strings.Space(15 - Strings.Len(VDaire));

                Vno = Strings.Mid(cariler.Rows[i][5].ToString(), 1, 15);
                if (Strings.Len(Vno) < 15)
                    Vno = Vno + Strings.Space(15 - Strings.Len(Vno));

                Discount = Strings.Format(Convert.ToInt32(cariler.Rows[i][6]), "00");

                Sorted_dataset.Tables["CARI"].Rows.Add(CariKod, Strings.Format(sira, "000000"));
                Toplam_Satir = "1" + CariId + CariKod + CariSatir + VDaire + Vno + Discount + Strings.Format(02, "00") + Strings.Space(20);
                sbCariFile.Append(Toplam_Satir + Environment.NewLine);
                sbCariNoFile.Append(CariId + Strings.Format(sira, "000000") + Environment.NewLine);
                sira = sira + 1;
            }
            AllStrings.Append(sbCariFile.ToString() + Environment.NewLine);
            AllStrings.Append("----------" + Environment.NewLine);
            AllStrings.Append(sbCariNoFile.ToString() + Environment.NewLine);
            AllStrings.Append("----------" + Environment.NewLine);
            DataView Sort_View = new DataView(Sorted_dataset.Tables["CARI"]);
            Sort_View.Sort = "ID ASC";

            for (int k = 0; k <= Sorted_dataset.Tables["CARI"].Rows.Count - 1; k++)
                sbCariKodFile.Append(Sort_View[k]["ID"].ToString() + Sort_View[k]["SIRANO"].ToString() + Environment.NewLine);
            AllStrings.Append(sbCariKodFile + Environment.NewLine);
        }
        catch
        {

        }
        return GZipCompress(AllStrings.ToString());
    }
    #endregion

    #region Urun Taksit
    public byte[] UrunTaksit(DataTable urunler)
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            string pluno = "";
            string stok_adi = "";
            decimal fiyat = 0;
            string convFiyat = "";
            string line = "";
            for (int i = 0; i < urunler.Rows.Count; i++)
            {
                pluno = urunler.Rows[i][0].ToString();
                stok_adi = urunler.Rows[i][1].ToString();
                fiyat = Convert.ToDecimal(urunler.Rows[i][2]);
                if (pluno.Length < 6)
                    pluno = sifir(6 - pluno.Length) + pluno;
                if (stok_adi.Length < 20)
                    stok_adi = boslukEkle(20 - stok_adi.Length, stok_adi);
                else
                    stok_adi = stok_adi.Substring(0, 20);
                convFiyat = fiyat.ToString().Replace(",", ".");
                if (convFiyat.IndexOf(".") > -1)
                {
                    if (convFiyat.Length > convFiyat.IndexOf(".") + 3)
                        convFiyat = convFiyat.Substring(0, convFiyat.IndexOf(".") + 3);
                }
                if (convFiyat.Length < 10)
                    convFiyat = sifir(10 - convFiyat.Length) + convFiyat;
                line = pluno + "," + stok_adi + "," + convFiyat;
                if (line.Length == 38)
                    sb.Append(line + System.Environment.NewLine);
            }
        }
        catch
        {
        }
        return GZipCompress(sb.ToString());
    }

    private string boslukEkle(int p, string stok_adi)
    {
        string stok = stok_adi;
        for (int i = 0; i < p; i++)
            stok += " ";
        return stok;
    }

    private string sifir(int p)
    {
        string sifirAdd = "";
        for (int i = 0; i < p; i++)
            sifirAdd += "0";
        return sifirAdd;
    }
    #endregion

    #region Urun Dat
    #region fields
    string urun_plno = "";
    string barkod = "";
    string urunad = "";
    string fiyat = "";
    string kdv = "";
    string birim = "";
    string tartili = "";
    #endregion
    public byte[] URUNDAT()
    {
        StringBuilder sb = new StringBuilder();
        StringBuilder sbPlno = new StringBuilder();
        StringBuilder sbBarkod = new StringBuilder();
        StringBuilder sbAllBuilder = new StringBuilder();
        barkod_ = new Db_Barkot();
        dt = barkod_.GetAllRecord_Datatable2("SELECT barkod_stok_id FROM TBL_0_BARKOD GROUP BY barkod_stok_id");
        DataTable dt2;
        DataSet dsBarkod = new DataSet();
        dsBarkod.Tables.Add("BARKODNO");
        dsBarkod.Tables.Add("PLNO");
        dsBarkod.Tables["BARKODNO"].Columns.Add("BARKOD");
        dsBarkod.Tables["BARKODNO"].Columns.Add("SATIRNO");
        dsBarkod.Tables["PLNO"].Columns.Add("URUNNO");
        dsBarkod.Tables["PLNO"].Columns.Add("SATIR");
        int sayac = 1;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt2 = barkod_.GetAllRecord_Datatable2("SELECT TBL_0_STOK.stok_id, TBL_0_BARKOD.barkod_kod, TBL_0_STOK.stok_adi, TBL_0_FIYAT.fiyat, TBL_0_STOK.stok_per_satis_kdv, TBL_0_BIRIM.birim_adi,TBL_0_BARKOD.barkod_icerik FROM TBL_0_BARKOD INNER JOIN TBL_0_STOK ON TBL_0_BARKOD.barkod_stok_id = TBL_0_STOK.stok_id INNER JOIN TBL_0_BIRIM ON TBL_0_STOK.stok_id = TBL_0_BIRIM.birim_stok_id INNER JOIN TBL_0_FIYAT ON TBL_0_STOK.stok_id = TBL_0_FIYAT.fiyat_stok_id WHERE (TBL_0_STOK.stok_id = " + int.Parse(dt.Rows[i][0].ToString()) + ") ORDER BY TBL_0_BARKOD.barkod_kod");
            if (dt2.Rows.Count > 0)
            {
                for (int i2 = 0; i2 < dt2.Rows.Count; i2++)
                {
                    urun_plno = Stokid(dt2.Rows[i2][0].ToString());
                    barkod = AdOrBarkodSpace(dt2.Rows[i2][1].ToString());
                    dsBarkod.Tables["BARKODNO"].Rows.Add(barkod, sayac);
                    dsBarkod.Tables["PLNO"].Rows.Add(urun_plno, sayac);
                    sayac++;
                    urunad = AdOrBarkodSpace(dt2.Rows[i2][2].ToString());
                    fiyat = Fiyat(dt2.Rows[i2][3].ToString());
                    kdv = Kdv(dt2.Rows[i2][4].ToString());
                    birim = dt2.Rows[i2][5].ToString();
                    tartili = Tartili(int.Parse(dt2.Rows[i2][6].ToString()));
                    sb.Append(urun_plno + barkod + urunad + fiyat + birim + tartili + Environment.NewLine);
                    CreateArea();
                }
            }
        }
        sbAllBuilder.Append(sb.ToString());
        sbAllBuilder.Append("----------" + Environment.NewLine);
        for (int i = 0; i < dsBarkod.Tables[0].Rows.Count; i++)
            sbBarkod.Append(dsBarkod.Tables[0].Rows[i][0].ToString() + dsBarkod.Tables[0].Rows[i][1].ToString() + Environment.NewLine);
        sbAllBuilder.Append(sbBarkod.ToString());
        sbAllBuilder.Append("----------" + Environment.NewLine);
        for (int i = 0; i < dsBarkod.Tables[1].Rows.Count; i++)
            sbPlno.Append(dsBarkod.Tables[1].Rows[i][0].ToString() + dsBarkod.Tables[1].Rows[i][1].ToString() + Environment.NewLine);
        sbAllBuilder.Append(sbPlno.ToString());
        return GZipCompress(sbAllBuilder.ToString());
    }
    public void CreateArea()
    {
        urun_plno = "";
        barkod = "";
        urunad = "";
        fiyat = "";
        kdv = "";
        birim = "";
        tartili = "";
    }
    public string Stokid(string id)
    {
        string sifirAdd = "";
        for (int i = 0; i < 6 - id.Length; i++)
            sifirAdd += "0";
        if (sifirAdd != "")
            id = sifirAdd + id;
        return id;
    }

    public string AdOrBarkodSpace(string barkod)
    {
        string brkd = "";
        for (int i = 0; i < 20 - barkod.Length; i++)
            brkd += " ";
        if (brkd != "")
            barkod = barkod + brkd;
        return barkod;
    }

    public string Fiyat(string fiyat)
    {
        string sifirAdd = "";
        for (int i = 0; i < 10 - fiyat.Length; i++)
            sifirAdd += "0";
        if (sifirAdd != "")
            fiyat = sifirAdd + fiyat;
        return fiyat;
    }

    public string Kdv(string kdv)
    {
        string sifiAdd = "";
        for (int i = 0; i < 2 - kdv.Length; i++)
            sifiAdd += "0";
        if (sifiAdd != "")
            kdv = sifiAdd + kdv;
        return kdv;
    }

    public string Tartili(int tarti)
    {
        string sonuc = "";
        if (tarti == 1 || tarti == 2)
            sonuc = "E";
        else
            sonuc = "H";
        return sonuc;
    }
    #endregion

    #region Depo No
    public DataTable DepoPos(string gelen)
    {
        pos = new Db_Poslar();
        dsDepoPos = new DataSet();
        if (gelen != "")
            dtDepoPos = pos.GetAllRecord_Datatable2("SELECT pos_rec_id, cash_id, cash_adi, cash_depo_id, cash_tip, cash_stok_exp, cash_sale_imp, cash_msg, cash_fatseri, cash_nakitkasa, cash_port_no,cash_yardimci_prg FROM TBL_0_POSLAR WHERE (cash_depo_id = " + int.Parse(gelen) + ")");
        return dtDepoPos;
    }
    #endregion
}
